<%@page import="study.itwill.team5.admin.AdminItem"%>
<%@page import="study.itwill.jsp.WebHandler"%>
<%@page import="study.itwill.team5.board.Board"%>
<%@page import="study.itwill.common.PatternChecker"%>
<%@page import="study.itwill.common.DBHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ include file="../session.jsp"%>
<%@ include file="../dbcon.jsp"%>
<%
	PatternChecker checker = PatternChecker.getInstance();

	// 모든 파라미터 받기
	int seq = web.getInt("seq");
	int board_category_seq = web.getInt("board_category_seq");
	int member_seq = web.getInt("member_seq");
	String title = web.getValue("title");
	
	int house_seq = 0;
	int grade = 0;
	
	if (web.getInt("house_seq") > 0 && web.getInt("grade") > 0) {
		house_seq = web.getInt("house_seq");
		grade = web.getInt("grade");
	}
	
	String content = web.getValue("content");
	
	if (seq == -1) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
	
	if (board_category_seq == -1) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
	
	if (member_seq == -1) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
	
	if (!checker.isValue(title)) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
	
	if (!checker.isValue(content)) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
%>
<%
	boolean result = false;
	
	if (house_seq > 0 && grade > 0) {
		/** 데이터 저장 */
		Board board = new Board();
		result = board.editBoard(seq, title, content, board_category_seq, member_seq, house_seq, grade);
		
		if (!result) {
			db.disconnect();
			web.goPage(null, "수정에 실패했습니다.");
			return;
		}
	} else {
		/** 데이터 저장 */
		Board board = new Board();
		result = board.editBoard(seq, title, content, board_category_seq, member_seq);
		
		if (!result) {
			db.disconnect();
			web.goPage(null, "수정에 실패했습니다.");
			return;
		}
	}

	/** 접속 해제 */
	db.disconnect();
	
	if (!result) {
		web.goPage(null, "수정에 실패했습니다.");
		return;
	}
	
	// 페이지 강제 이동 (JSP)
	web.goPage("viewBoard.jsp?seq=" + seq, null);
%>